{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Result Visualizer"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Different visualizations of anomaly detection result, including forecasts, reconstructions, anomaly scores, predicted and actual anomalies"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import plotly.express as px\n",
    "import os\n",
    "import pickle\n",
    "from sklearn.metrics import f1_score, precision_score, recall_score\n",
    "import plotly as py\n",
    "from plotly.offline import download_plotlyjs, init_notebook_mode\n",
    "import plotly.graph_objs as go\n",
    "import cufflinks as cf\n",
    "import glob\n",
    "import sys\n",
    "import json\n",
    "import more_itertools as mit\n",
    "from tqdm.notebook import tqdm\n",
    "cf.go_offline()\n",
    "init_notebook_mode\n",
    "\n",
    "sys.path.insert(0, '.')\n",
    "from plotting import Plotter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from IPython.core.display import display, HTML\n",
    "display(HTML(\"<style>.container { width:95% !important; }</style>\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Provide what output to visualize\n",
    "In case of multiple models trained for the same dataset, they will be saved in different folders named using the datetime of training.\n",
    "Use 'model_id' to specify which to load, '-1' for latest, '-2' for second latest, etc, or you can specify the exact datetime. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "res_path = './output/MSL'\n",
    "#res_path = './output/SMAP'\n",
    "#res_path = './output/SMD/1-1'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "plotter = Plotter(res_path, model_id='-1')\n",
    "plotter.result_summary()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Interactive plot \n",
    "- Interactive plot of forecastings, reconstructions,actual values, errors, etc of the specified feature.\n",
    "- Params:\n",
    "    - ```feature```: which feature to visualize\n",
    "    - ```plot_train```: whether to also plot for train set\n",
    "    - ```plot_errors```: whether to plot anomaly scores (error) for the feature\n",
    "    - ```plot_feature_anom```: if True, the feature-wise threshold and anomaly prediction is included\n",
    "    - ```start```: start index of dataset\n",
    "    - ```end```: end index of dataset\n",
    "\n",
    "If ```plot_feature_anom``` is set to True:\n",
    "- Predicted anomalies (for the particular feature) are visualized by a light blue rectangle.\n",
    "- As we only predict one feature for SMAP and MSL, the actual (true) anomalies are included using a red rectangle, making correctly predicted anomalies purple.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "plotter.plot_feature(\n",
    "    feature=0, \n",
    "    plot_train=True, \n",
    "    plot_errors=True, \n",
    "    plot_feature_anom=True,\n",
    "    start=350, \n",
    "    end=3000\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Global (entity-level) Anomaly Predictions \n",
    "```plot_global_predictions``` and ```plotly_global_predictions``` visualize the entity-level anomaly scores which are used to make anomaly predictions at entity-level.\n",
    "In ```plotly_global_predictions```:\n",
    "- Actual (true) anomalies are visualized by a red rectangle\n",
    "- Predicted anomalies are visualized by a blue rectangle. \n",
    "- Thus, if anomalies are predicted correctly the color of the rectangle will be purple. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "plotter.plot_global_predictions(type=\"test\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "plotter.plotly_global_predictions(type=\"test\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# plotter.plot_all_features(type='test')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
